package com.jqr.activity.viewpagerfragmentdemo2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jqr.activity.viewpagerfragmentdemo2.fragments.OneFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager2 viewPager2;

    private LinearLayout llTabWeiXin,llTabContact,llTabFind,llTabMe;

    private ImageView ivWeiXin,ivContact,ivFind,ivMe,ivCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPager();

        initBottom();

    }

    private void initBottom() {

        llTabWeiXin=findViewById(R.id.tab_weixin);
        llTabWeiXin.setOnClickListener(this);
        llTabContact=findViewById(R.id.tab_contact);
        llTabContact.setOnClickListener(this);
        llTabFind=findViewById(R.id.tab_find);
        llTabFind.setOnClickListener(this);
        llTabMe=findViewById(R.id.tab_me);
        llTabMe.setOnClickListener(this);

        ivWeiXin=findViewById(R.id.tab_iv_weixin);
        ivContact=findViewById(R.id.tab_iv_contact);
        ivFind=findViewById(R.id.tab_iv_find);
        ivMe=findViewById(R.id.tab_iv_me);
        ivWeiXin.setSelected(true);
        ivCurrent=ivWeiXin;

    }

    private void initPager() {
        viewPager2=findViewById(R.id.container);

        List<Fragment> fragmentList=new ArrayList<>();
        fragmentList.add(OneFragment.newInstance("微信"));
        fragmentList.add(OneFragment.newInstance("通信录"));
        fragmentList.add(OneFragment.newInstance("发现"));
        fragmentList.add(OneFragment.newInstance("我的"));
        MyFragmentPagerAdapter myFragmentPagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragmentList);

        viewPager2.setAdapter(myFragmentPagerAdapter);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        switch (position){
            case R.id.tab_weixin:
                viewPager2.setCurrentItem(0);
            case 0:

                ivWeiXin.setSelected(true);
                ivCurrent=ivWeiXin;
                break;
            case R.id.tab_contact:
                viewPager2.setCurrentItem(1);
            case 1:

                ivContact.setSelected(true);
                ivCurrent=ivContact;
                break;
            case R.id.tab_find:
                viewPager2.setCurrentItem(2);
            case 2:

                ivFind.setSelected(true);
                ivCurrent=ivFind;
                break;
            case R.id.tab_me:
                viewPager2.setCurrentItem(3);
            case 3:
                ivMe.setSelected(true);
                ivCurrent=ivMe;
                break;
        }
    }

    @Override
    public void onClick(View v) {
        changeTab(v.getId());
    }
}